﻿using SkiaSharp;

namespace Service.Core.Utils.VerificationCodeUtil
{
    /// <summary>
    /// 字母数字混合图片验证码
    /// </summary>
    public partial class HybridImgVerificationCodeService : IVerificationCodeService
    {
        private static readonly List<SKColor> _colors =
        [
            SKColors.Purple,SKColors.Red,SKColors.DarkBlue,SKColors.DarkRed,SKColors.Black,SKColors.Blue
        ];

        public async Task<VerfyCode> CreateVerificationCode()
        {
            return await Task.FromResult(CreateCodeImage());
        }

        /// <summary>
        /// 3.混合验证码
        /// </summary>
        /// <param name="length">字符长度</param>
        /// <returns>验证码字符</returns>
        private string CreateMixVerifyCode(int length)
        {
            char[] verification = new char[length];
            char[] dictionary = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
   };
            Random random = new Random();
            for (int i = 0; i < length; i++)
            {
                verification[i] = dictionary[random.Next(dictionary.Length - 1)];
            }
            return new string(verification);
        }

        public VerfyCode CreateCodeImage(int width = 100, int height = 40, int lineNum = 4, int lineStrookeWidth = 2, int length = 4)
        {
            var verifyCode = string.Empty;
            verifyCode = CreateMixVerifyCode(length);
            //创建bitmap位图
            using SKBitmap image2d = new SKBitmap(width, height, SKColorType.Bgra8888, SKAlphaType.Premul);
            //创建画笔
            using SKCanvas canvas = new SKCanvas(image2d);
            //填充背景颜色
            canvas.DrawColor(SKColors.LightGray);
            var array = verifyCode.ToArray();
            Random random = new Random();
            for (int i = 0; i < array.Length; i++)
            {
                //将文字写到画布上
                using SKPaint drawStyle = CreatePaint(random.Next(_colors.Count), 30);
                using SKFont font = CreateFont(30);
                canvas.DrawText(array[i].ToString(), i * 24 + 1, height - 10, font, drawStyle);
            }
            //画随机干扰线
            using (SKPaint drawStyle = new SKPaint())
            {

                for (int i = 0; i < lineNum; i++)
                {
                    drawStyle.Color = _colors[random.Next(_colors.Count)];
                    drawStyle.StrokeWidth = lineStrookeWidth;
                    canvas.DrawLine(random.Next(0, width), random.Next(0, height), random.Next(0, width), random.Next(0, height), drawStyle);
                }
            }
            //返回图片byte
            using SKImage img = SKImage.FromBitmap(image2d);
            using SKData p = img.Encode(SKEncodedImageFormat.Jpeg, 100);
            return new VerfyCode() { VerfyByte = p.ToArray(), Code = verifyCode };
        }
        // 创建画笔
        private SKPaint CreatePaint(int colorNo, float fontSize)
        {
            SKFontStyle sKFontStyle = new SKFontStyle(SKFontStyleWeight.Normal, SKFontStyleWidth.Normal, SKFontStyleSlant.Italic);
            SKFont font = new SKFont(SKTypeface.FromFamilyName("微软雅黑", sKFontStyle), fontSize);
            SKPaint paint = new SKPaint();
            paint.IsAntialias = true;
            paint.Color = _colors[colorNo];
            return paint;
        }

        private SKFont CreateFont(float fontSize)
        {
            SKFontStyle sKFontStyle = new SKFontStyle(SKFontStyleWeight.Normal, SKFontStyleWidth.Normal, SKFontStyleSlant.Italic);
            SKFont font = new SKFont(SKTypeface.FromFamilyName("微软雅黑", sKFontStyle), fontSize);
            return font;
        }
    }
}
